package com.ql.service.doctor.impl;
import com.ql.dao.yk.DoctorMapper;

import com.ql.pojo.yk.Doctor;
import com.ql.service.doctor.DoctorService;
import com.ql.utils.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("doctorService")
public class DoctorServiceImpl implements DoctorService {

    private static final Logger LOGGER = Logger.getLogger(DoctorServiceImpl.class);

    @Resource
    private DoctorMapper doctorMapper;

    public List<Doctor> getAllDoctor(PageHelper ph,String qryName,int qryRoleId) {
        List<Doctor> doctorList = new ArrayList<>();
        try {
            //add:更新总条数,
            //add:增加分页的条件
            ph.setTotalCount(doctorMapper.qryCount(qryName,qryRoleId));

            doctorList = doctorMapper.qryAll((ph.getCurrentPageNo()-1)*ph.getPageSize(),ph.getPageSize(),qryName,qryRoleId);
            if (doctorList.size()<=0){
                LOGGER.debug("DoctorServiceImpl.getAllDoctor 查询为空！");
            }else{
                LOGGER.debug("DoctorServiceImpl.getAllDoctor 查询成功！");
            }
        } catch (Exception e) {
            LOGGER.error("DoctorServiceImpl.getAllDoctor 异常:"+e.getMessage());
        }
        return doctorList;
    }

    public Doctor getDoctorById(String doctorId) {
        Doctor doctor = null;
        try {
            doctor = doctorMapper.qryById(doctorId);
            if (null == doctor){
                LOGGER.debug("DoctorServiceImpl.getDoctorById 为空");
            }else {
                LOGGER.debug("DoctorServiceImpl.getDoctorById 成功");
            }
        } catch (Exception e) {
            LOGGER.error("DoctorServiceImpl.getDoctorById 通过ID查询医生信息异常》》》"+e.getMessage());
            doctor = new Doctor();
        }
        return doctor;
    }

    @Override
    public Doctor selectById(Integer id) {
        return doctorMapper.selectByPrimaryKey(id);
    }

    @Override
    public void xiugaiDoctor(Doctor doctor) {
        doctorMapper.updateByPrimaryKey(doctor);
    }

    @Override
    public boolean addNewDoctor(Doctor doctor) {
        int row = 0;
        try {
            row = doctorMapper.save(doctor);
            if (row!=1){
                LOGGER.debug("DoctorServiceImpl.addNewDoctor 新增医生信息失败！");
                return  false;
            }else{
                LOGGER.debug("DoctorServiceImpl.addNewDoctor 新增医生信息成功！");
                return  true;
            }
        } catch (Exception e) {
            LOGGER.error("DoctorServiceImpl.addNewDoctor 新增医生信息异常"+e.getMessage());
            return false;
        }

    }

    @Override
    public boolean updateDoctor(Doctor doctor) {
        boolean updDoctor = false;
        System.out.println(doctor.getDeparturetime());
        try {
            int row = doctorMapper.modify(doctor);
            if (row == 0){
                LOGGER.debug("DoctorServiceImpl.updateDoctor 修改信息失败！");
            }else {
                LOGGER.debug("DoctorServiceImpl.updateDoctor 修改信息成功！");
                updDoctor = true;
            }
        } catch (Exception e) {
            LOGGER.error("DoctorServiceImpl.updateDoctor 修改信息异常》》》》"+e.getMessage());
        }
        return updDoctor;
    }

    //    根据科室id获取科室医师列表
    @Override
    public List<Doctor> qryByKid(int kId) {
        List<Doctor> doctors = null;
        try {
            doctors = doctorMapper.qryByKid( kId );
        } catch (Exception e) {
            LOGGER.error( "DoctorServiceImpl.qryByKid 修改信息异常》》》》"+e.getMessage() );
        }
        return doctors;
    }
}
